﻿using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Interactivity;
using MusicFluent.Models;
using MusicFluent.ViewModels.Pages;
using MusicFluent.Views;

namespace MusicFluent.Pages;

public partial class FindView : PageViewBase
{
    private List<string> HighQualityTags { get; set; } = [];

    public FindView()
    {
        InitializeComponent();
    }

    protected override async void OnInitialized()
    {
        var main = ViewOpera.GetView<MainWindow>(this);
        main?.TaskShow();
        if (DataContext is not FindViewModel model) return;
        HighQualityTags = await model.Playlist.HighQualityTag();
        HighQualityTags.Insert(0, "全部");
        var strip = HighQualityTags[..(HighQualityTags.Count > 10 ? 10 : ^1)];
        strip.Add("...");
        Strip.ItemsSource = strip;
        MoreTagItems.ItemsSource = HighQualityTags[(HighQualityTags.Count > 10 ? 10 : ^1)..];
        Strip.SelectedValue = 0;
        main?.TaskShow();
    }

    private async void StripSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (DataContext is not FindViewModel model) return;
        var s = (Strip.SelectedValue as string)!;
        if (s == "...")
        {
            MoreTagBorder.IsVisible = true;
        }
        else
        {
            var main = ViewOpera.GetView<MainWindow>(this);
            main?.TaskShow();
            Items.ItemsSource = await model.Playlist.HighQuality(s);
            MoreTagBorder.IsVisible = false;
            main?.TaskShow();
        }
    }

    private async void TabClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not FindViewModel model) return;
        if (sender is not Control control) return;
        if (control.DataContext is not string s) return;
        var main = ViewOpera.GetView<MainWindow>(this);
        main?.TaskShow();
        Items.ItemsSource = await model.Playlist.HighQuality(s);
        main?.TaskShow();
    }
}